
package com.bei_en.teacher.management.controller.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bei_en.teacher.management.controller.BaseController;
import com.bei_en.teacher.management.model.system.condition.SystemRoleCondition;
import com.bei_en.teacher.management.model.system.condition.SystemRoleResourceCondition;
import com.bei_en.teacher.management.model.system.vo.SystemResourceRespVo;
import com.bei_en.teacher.management.model.system.vo.SystemRoleVo;
import com.bei_en.teacher.management.model.system.vo.SystemUserVo;
import com.bei_en.teacher.management.service.system.SystemLogsService;
import com.bei_en.teacher.management.service.system.SystemResourceService;
import com.bei_en.teacher.management.service.system.SystemRoleResourceService;
import com.bei_en.teacher.management.service.system.SystemRoleService;
import com.bei_en.teacher.management.web.MyJsonFailResult;
import com.bei_en.teacher.management.web.MyJsonResult;
import com.bei_en.teacher.management.web.MyJsonSuccessResult;

/**
 * 后台角色
 * @author Longqs
 * @date 2016-6-15 14:55:59
 */
@Controller
@RequestMapping("/admin")
public class SystemRoleController extends BaseController {

    @Autowired
    SystemRoleService systemRoleService;
    @Autowired
    SystemLogsService systemLogsService;
    @Autowired
    SystemRoleResourceService systemRoleResourceService;
    @Autowired
    SystemResourceService systemResourceService;

    private void addSharedAttr(Model model) {
	model.addAttribute("statusList", gson.fromJson("{1:'正常',0:'删除'}", Map.class));
    }

    @RequestMapping("/systemRole")
    public String systemRole(Model model, SystemRoleCondition vo, HttpServletResponse resp) {

	List<SystemRoleVo> list = new ArrayList<SystemRoleVo>();
	try {
	    list = systemRoleService.findSystemRolePage(vo);
	} catch (Exception e) {
	    e.printStackTrace();
	    writeJson(resp, new MyJsonFailResult(e.getMessage()));
	    return null;
	}

	model.addAttribute("list", list);
	addSharedAttr(model);
	model.addAttribute("vo", vo);

	return "/admin/system/roleList.html";
    }

    @RequestMapping("/systemRoleAdd")
    public String systemRoleAdd(Model model, SystemRoleVo vo, boolean show, HttpSession session,
	    HttpServletResponse resp) {

	if (show) {

	    List<SystemResourceRespVo> list = systemResourceService.findAllResp();
	    model.addAttribute("resList", list);

	    addSharedAttr(model);
	    return "/admin/system/roleAdd.html";
	}
	try {
	    vo.setCreateTime(new Date());
	    systemRoleService.insert(vo);

	    SystemUserVo obj = getLoginUser(session);
	    String content = "新增角色" + vo.getRoleName();
	    logsInsert(obj.getId(), content, 1);

	} catch (Exception e) {
	    e.printStackTrace();
	    writeJson(resp, new MyJsonFailResult(e.getMessage()));
	    return null;
	}
	MyJsonSuccessResult re = new MyJsonSuccessResult();
	re.setCloseCurrent(true);
	re.setTabid("systemRoleList");
	writeJson(resp, re);
	return null;
    }

    @RequestMapping("/systemRoleEdit")
    public String systemRoleEdit(Model model, boolean show, SystemRoleVo vo, HttpSession session,
	    HttpServletResponse resp) {

	if (show) {
	    SystemRoleVo vo1 = systemRoleService.findById(vo.getId());

	    SystemRoleResourceCondition cvo = new SystemRoleResourceCondition();
	    cvo.setRoleId(vo.getId());
	    cvo.setPageSize(Integer.MAX_VALUE);
	    cvo.setOrderField("resource_id");
	    cvo.setOrderDirection("asc");
	    vo1.setResourceList(systemRoleResourceService.findSystemRoleResourcePageString(cvo));

	    List<SystemResourceRespVo> list = systemResourceService.findAllResp();
	    model.addAttribute("resList", list);

	    addSharedAttr(model);
	    model.addAttribute("vo", vo1);
	    return "/admin/system/roleEdit.html";
	}
	try {
	    systemRoleService.updateSelective(vo);

	    SystemUserVo obj = getLoginUser(session);
	    String content = "编辑角色" + vo.getRoleName();
	    logsInsert(obj.getId(), content, 2);

	} catch (Exception e) {
	    e.printStackTrace();
	    writeJson(resp, new MyJsonFailResult(e.getMessage()));
	    return null;
	}
	MyJsonSuccessResult re = new MyJsonSuccessResult();
	re.setCloseCurrent(true);
	re.setTabid("systemRoleList");
	writeJson(resp, re);
	return null;
    }

    @RequestMapping("/systemRoleDel")
    @ResponseBody
    public MyJsonResult systemRoleDel(Model model, int[] delids, HttpSession session) {

	try {
	    for (int i : delids) {
		SystemRoleVo vo1 = systemRoleService.findById(i);
		vo1.setStatus(0);
		systemRoleService.updateSelective(vo1);

		SystemUserVo obj = getLoginUser(session);
		String content = "删除角色" + vo1.getRoleName();
		logsInsert(obj.getId(), content, 3);

	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    return new MyJsonFailResult(e.getMessage());
	}
	return new MyJsonSuccessResult();
    }

}
